<?php
/**
 * 订单取消.
 * User: madong
 * Date: 2018/10/16
 * Time: 5:37 AM
 */

namespace app\api\controller\jiaoyi;


use think\Controller;
use think\Db;
use think\Request;

class Quxiao extends Controller
{

    /**
     * 订单取消
     *
     * 参数
     *
     * @ordernumber  当前订单号
     *
     * @uname  当前用户名
     *
     * @upname  要释放的用户
     *
     * @uordernumber  对方订单号
     */
    public function Order_quxiao(){

        $res = Request::instance();

        $order_info = Db::name('jiaoyibuylog')->where('ordernumber',$res->param('ordernumber'))->find();

        if ($order_info['oorderzt'] == 4){echo json_encode(['zt'=>0,'msg'=>'您已取消过该订单']);die();}

        if ($res->isPost()){

            Db::startTrans();

            try{

                Db::name('jiaoyibuylog')->where('ordernumber',$res->param('ordernumber'))->update([

                    'oorderzt' => 4,

                    'quxiaoinfo' => $res->param('uname'),

                ]);

                Db::name('jiaoyi_uinfo')->where('uname',$res->param('upname'))->setInc('quxiaocs',1);

                Db::name('jiaoyi_uinfo')->where('uname',$res->param('upname'))->setDec('d'.$order_info['ubizhong'],$order_info['ucoinnum']);

                Db::name('jiaoyi_uinfo')->where('uname',$res->param('upname'))->setInc($order_info['ubizhong'],$order_info['ucoinnum']);

                //Db::name('jiaoyi')->where('ordernumber',$res->param('uordernumber'))->setDec('wfkdd',1);

                Db::name('jiaoyidj')->where('ddhao',$res->param('ordernumber'))->update([

                    'sftime' => time(),

                    'djzt' => 1,

                    'djztgb' => '用户'.$res->param('uname').'取消订单释放'.$order_info['ubizhong'].'数量'.$order_info['ucoinnum'].'个'

                ]);

                echo json_encode(['zt' => 1,'msg'=>'订单取消成功']);

                Db::commit();

            } catch (\Exception $e) {

                echo json_encode(['zt'=>0,'msg'=>'订单取消失败,请稍后重试']);

                Db::rollback();
            }

        }else{

            echo json_encode(['zt' =>2,'msg'=>'error']);

        }

    }

    //定时取消

    public function Order_time(){

        $res = Request::instance();

        if ($res->isPost()){

            //订单到期时间，这是用户前端用户设置的订单有效时间 不是时间戳格式是分钟数
            $dqsj = Db::name('jiaoyibuylog')->where('ordernumber',$res->param('ordernumber'))->value('dqsj');

            //下单时间
            $xdsj = Db::name('jiaoyibuylog')->where('ordernumber',$res->param('ordernumber'))->value('time');

            $fen = $dqsj;


            //传入前端的剩余秒数

            $apptime = strtotime('+'.$fen.'minute',$xdsj)-time();

            if ($apptime>0){//如果剩余的时间戳大于0就是没用到时间

                echo json_encode(['zt'=>1,'msg'=>$apptime]);

            }else{//如果返回的时间戳小于0那就是订单到期了，执行订单取消操作

                echo json_encode(['zt'=>0,'msg'=>'']);

            }

        }else{

            echo json_encode(['zt'=>0,'msg'=>'error']);

        }

    }

    //服务器触发订单取消(处理遗漏)

    public function Server_quxiao(){



    }

}